package com.lp.business.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.hibernate.dao.BaseHibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.lp.business.model.Report;

/**
 * 报告信息模型数据访问层
 * 
 * @author lp
 * @email lp@qq.com
 * @date 2018-08-23 10:38:18
 */
@Repository("reportDao")
public class ReportDao extends BaseHibernateDaoSupport<Report, String> {
	@Override
	public List<Report> findByModel(Report model, Order order, Pager pager) {
		model = model == null ? new Report() : model;
		StringBuffer hql = new StringBuffer();
		hql.append("from Report c where 1=1");
		hql.append(V.isNotEmpty(model.getId()) ? " and c.id='" + model.getId() + "' " : "");
		hql.append(V.isNotEmpty(model.getStatus()) ? " and c.status='" + model.getStatus() + "' " : "");
		hql.append(V.isNotEmpty(model.getVersion()) ? " and c.version=" + model.getVersion() + " " : "");
		hql.append(V.isNotEmpty(model.getAuthor()) ? " and c.author='" + model.getAuthor() + "' " : "");
		hql.append(V.isNotEmpty(model.getCover()) ? " and c.cover='" + model.getCover() + "' " : "");
		hql.append(V.isNotEmpty(model.getDescription()) ? " and c.description='" + model.getDescription() + "' " : "");
		hql.append(V.isNotEmpty(model.getFilePath()) ? " and c.filePath='" + model.getFilePath() + "' " : "");
		hql.append(V.isNotEmpty(model.getIndustries()) ? " and c.industries like '%" + model.getIndustries() + "%' " : "");
		hql.append(V.isNotEmpty(model.getName()) ? " and c.name='" + model.getName() + "' " : "");
		hql.append(V.isNotEmpty(model.getPrice()) ? " and c.price=" + model.getPrice() + " " : "");
		hql.append(V.isNotEmpty(model.getDateStart()) && V.isNotEmpty(model.getDateEnd()) ? " and (c.createTime between '" + DateFormatUtils.format(model.getDateStart(), "yyyy-MM-dd HH:mm:ss") + "' and '" + DateFormatUtils.format(model.getDateEnd(), "yyyy-MM-dd HH:mm:ss") + "') " : "");
		hql.append(order != null ? order.toString() : "");
		return this.findByQueryString(hql.toString(),model, pager);
	}
	
	public List<Map<String, Object>> findReports(Report model, Order order, Pager pager,String memberId) {
		model = model == null ? new Report() : model;
		StringBuffer hql = new StringBuffer();
		hql.append("select id,name,cover,createTime,industries ");
		hql.append(",(select count(1) from look l where l.reportId = c.id) looktimes ");
		hql.append(",(SELECT IF(COUNT(1)>=1,1,0) FROM collection co WHERE co.reportId = c.id AND co.memberId = '"+memberId+"') isCollection ");
		hql.append("from report c where 1=1");
		hql.append(V.isNotEmpty(model.getId()) ? " and c.id='" + model.getId() + "' " : "");
		hql.append(V.isNotEmpty(model.getStatus()) ? " and c.status='" + model.getStatus() + "' " : "");
		hql.append(V.isNotEmpty(model.getVersion()) ? " and c.version=" + model.getVersion() + " " : "");
		hql.append(V.isNotEmpty(model.getAuthor()) ? " and c.author='" + model.getAuthor() + "' " : "");
		hql.append(V.isNotEmpty(model.getCover()) ? " and c.cover='" + model.getCover() + "' " : "");
		hql.append(V.isNotEmpty(model.getDescription()) ? " and c.description='" + model.getDescription() + "' " : "");
		hql.append(V.isNotEmpty(model.getFilePath()) ? " and c.filePath='" + model.getFilePath() + "' " : "");
		hql.append(V.isNotEmpty(model.getIndustries()) ? " and c.industries like '%" + model.getIndustries() + "%' " : "");
		hql.append(V.isNotEmpty(model.getName()) ? " and c.name='" + model.getName() + "' " : "");
		hql.append(V.isNotEmpty(model.getPrice()) ? " and c.price=" + model.getPrice() + " " : "");
		hql.append(V.isNotEmpty(model.getDateStart()) && V.isNotEmpty(model.getDateEnd()) ? " and (c.createTime between '" + DateFormatUtils.format(model.getDateStart(), "yyyy-MM-dd HH:mm:ss") + "' and '" + DateFormatUtils.format(model.getDateEnd(), "yyyy-MM-dd HH:mm:ss") + "') " : "");
		hql.append(order != null ? order.toString() : "");
		L.i(hql.toString());
		return this.querySQL(hql.toString(), pager);
	}
	
}